package com.juneng.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.juneng.common.R;
import com.juneng.dto.OrderRefundResponse;
import com.juneng.dto.OrderUserQueryDto;
import com.juneng.entity.OrderMsg;
import com.juneng.utils.ExcelUtil;
import com.juneng.utils.Func;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.juneng.vo.OrderUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import com.juneng.entity.OrderRefund;
import com.juneng.dto.OrderRefundDto;
import com.juneng.dto.OrderRefundQueryDto;
import com.juneng.vo.OrderRefundVo;
import com.juneng.service.IOrderRefundService;

import javax.servlet.http.HttpServletResponse;

/**
 * 订单商品退款 控制器
 *
 * @author zds
 * @since 2021-08-04
 */
@RestController
@AllArgsConstructor
@RequestMapping("orderRefund")
@Api(tags = "订单商品退款相关接口")
public class OrderRefundController {

	private IOrderRefundService orderRefundService;


	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<OrderRefundResponse> detail(Long orderRefundId) {
		OrderRefundResponse detail = orderRefundService.getById(orderRefundId);
		return R.success(detail);
	}

	/**
	 * 分页 订单商品退款
	 */
	@GetMapping("/page")
	@ApiOperation(value = "分页", notes = "传入orderRefund")
	public R<IPage<OrderRefundVo>> page(OrderRefundQueryDto dto) {
		IPage<OrderRefundVo> pages = orderRefundService.page(dto);
		return R.success(pages);
	}

	@PostMapping("/export")
	@ApiOperation(value = "导出excel")
	public void export(HttpServletResponse response, @RequestBody OrderRefundQueryDto dto) {
		List<String> titleList = Arrays.asList(
				"序号",
				"内部退单号",
				"内部订单号",
				"支付订单号",
				"退单订单号",
				"下单用户手机号",
				"支付方式",
				"订单金额",
				"支付金额",
				"退款金额",
				"退款原因",
				"退款地址",
				"退款详细地址",
				"订单进度",
				"商品信息",
				"生成时间"
		);
		List<List<Object>> dataList = new ArrayList<>();
		List<OrderRefundVo> orderRefundList = orderRefundService.getOrderRefundList(dto);
//        IPage<OrderUserVo> pages = orderUserService.page(dto);
//        List<OrderUserVo> exportOrder = pages.getRecords();
		//  List<OmsOrderVo> exportOrder = omsOrderService.getExportOrder(dto);
		for (OrderRefundVo orderRefundVo : orderRefundList) {
			ArrayList<Object> list = new ArrayList<>();
			list.add(orderRefundVo.getOutRefundNo());
			list.add(orderRefundVo.getInternalOrder());
			list.add(orderRefundVo.getPayOrder());
			list.add(orderRefundVo.getRefundSn());
			list.add(orderRefundVo.getPhone());
			if (orderRefundVo.getPaymentType() == 0){
				list.add("支付宝");
			}else {
				list.add("微信");
			}
			list.add(orderRefundVo.getTotalAmount());
			list.add(orderRefundVo.getPayAmount());
			list.add(orderRefundVo.getRefundAmount());
			list.add(orderRefundVo.getRefundReason());
			list.add(orderRefundVo.getConsigneeAddress());
			list.add(orderRefundVo.getConsigneeAddressDetail());
			switch (orderRefundVo.getRefundState()){
				// '进度：1申请中；2已退款；3退款失败；4已退款 5 已完成',
				case 1 : list.add("申请中"); break;
				case 2 : list.add("已退款");break;
				case 3 : list.add("退款失败");break;
				case 4 : list.add("已退款");break;
				case 5 : list.add("已完成");break;
			}
			//商品信息
			List<OrderMsg> product = orderRefundVo.getProduct();
			StringBuffer stringBuffer = new StringBuffer();
			if (product != null){
				for (OrderMsg orderMsg : product) {
					stringBuffer.append("商品名字："+orderMsg.getBookName()).append("/").append("价格："+orderMsg.getBookPrice()).append("数量；"+orderMsg.getOrderCount()).append("----");
				}
			}
			list.add(stringBuffer);
			list.add(orderRefundVo.getCreateDate());
			dataList.add(list);
		}
		ExcelUtil.uploadExcelAboutUser(response, "order.xlsx", titleList, dataList);
	}

	/**
	 * 列表 订单商品退款
	 */
	@GetMapping("/list")
	@ApiOperation(value = "不分页", notes = "传入orderRefund")
	public R<List<OrderRefund>> list(OrderRefundQueryDto dto) {
		List<OrderRefund> list = orderRefundService.list(dto);
		return R.success(list);
	}

	/**
	 * 新增 订单商品退款
	 */
	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入orderRefund")
	public R save(@RequestBody OrderRefundDto dto) {
		return R.success(orderRefundService.save(dto));
	}

	/**
	 * 修改 订单商品退款
	 */
	@PutMapping("/update")
	@ApiOperation(value = "修改", notes = "传入orderRefund")
	public R update(@RequestBody OrderRefundDto dto) {
		return R.success(orderRefundService.updateById(dto));
	}

	/**
	 * 删除 订单商品退款
	 */
	@DeleteMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.success(orderRefundService.deleteLogic(Func.toIntList(ids)));
	}
}
